package com.edu.modules.baseinfo.api;


import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.edu.modules.baseinfo.model.LoginModel;
import com.edu.modules.baseinfo.model.RegisterModel;
import com.edu.modules.baseinfo.service.AccountService;
import com.edu.modules.baseinfo.vo.*;
import com.sari.common.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

/**
 *
 * @author kwj
 * @version 2016-08-01
 */
@CrossOrigin
@Controller
@RequestMapping(value = "account")
public class AccountApi {

    private static Logger log = LoggerFactory.getLogger(AccountApi.class);

    @Autowired
    private AccountService accountService;

    @ResponseBody
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public RegisterResultVo register(HttpServletRequest request){

        JSONObject param = RequestContextUtils.readJson(request);
        return accountService.register(param);
    }

    @ResponseBody
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public LoginResultVo login(HttpServletRequest request,HttpServletResponse response){

        JSONObject param = RequestContextUtils.readJson(request);
        return accountService.login(param);

    }

    @ResponseBody
    @RequestMapping(value = "/updatePwd",method = RequestMethod.POST)
    public ResultVo updatePwd(HttpServletRequest request){

        JSONObject param = RequestContextUtils.readJson(request);
        return accountService.updatePwd(param);
    }
    /**
     * 获取图形验证码 将返回一个包含4位字符（字母或数字）的图形验证码，
     * 并且将图形验证码的值设置到用户的 session 中
     * @param time     时间戳
     * @param response 返回的 HttpServletResponse 响应
     */
    @RequestMapping(value = "checkCode/{time}", method = RequestMethod.GET)
    public void getCheckCode(@PathVariable("time") String time, HttpServletResponse response,
                             HttpServletRequest request) {
        BufferedImage checkCodeImage = null;
        String checkCodeString = null;
        // 获取图形验证码
        Map<String, Object> checkCode = CaptchaGenerator.generateCaptcha();

        if (checkCode != null) {
            checkCodeString = (String) checkCode.get("captchaString");
            checkCodeImage = (BufferedImage) checkCode.get("captchaImage");
        }

        if (checkCodeString != null && checkCodeImage != null) {
            try (ServletOutputStream outputStream = response.getOutputStream()) {
                // 设置 Session
                HttpSession session = request.getSession();
                session.setAttribute("checkCode", checkCodeString);
                // 将验证码输出
                ImageIO.write(checkCodeImage, "png", outputStream);
                response.setHeader("Pragma", "no-cache");
                response.setHeader("Cache-Control", "no-cache");
                response.setDateHeader("Expires", 0);
                response.setContentType("image/png");
            } catch (IOException e) {
                log.error("fail to get the ServletOutputStream");
            }
        }
    }

    @ResponseBody
    @RequestMapping(value = "/code",method = RequestMethod.GET)
    public ResultCodeVo getMsgCode(String phone){

        return new ResultCodeVo(accountService.getMsgCode(phone));
    }

    @ResponseBody
    @RequestMapping(value = "/studentId",method = RequestMethod.GET)
    public StudentIdVo getStudentId(String code){

        return accountService.getStudentId(code);
    }
}
